package orest.back.userneeds;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ssta.common.Constants;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.RecordSet;
import net.ssta.core.db.Record;
import net.ssta.core.web.RequestKit;

public class UserNeedsServlet extends ServletBase {

	protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
			System.out.println("UserNeedsServlet default Action");
	}
	
	
	
	/**
     * 获取用户项目需求列表
     * 
     * @param request
     * @param response
     */
    public void userKjcjNeedsList(HttpServletRequest request,
        HttpServletResponse response)
    {
    	try {
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        
		        Record searchRcd = requestKit.getRecordWithPrefix("");
		        
		        Map fildOptionMap = new HashMap();
		        
		        
		        UserNeedsHandler unhandler = new UserNeedsHandler();
		        RecordSet kjcgNeedsSet = unhandler.userKjcjNeedsList(requestKit, pagingInfo);
		        
		        RecordSet newEnerClass = unhandler.getFildOption("common","NewEnergyClass");
		        for(int i=0;i<newEnerClass.size();i++){
		        	Record rec = newEnerClass.get(i);
		        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
		        }
		        
		        RecordSet needReason = unhandler.getFildOption("T_NeedForAchieve","NeedReason");
		        for(int i=0;i<needReason.size();i++){
		        	Record rec = needReason.get(i);
		        	fildOptionMap.put("NEEDREASON"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
		        }
		        
		        RecordSet cooperate = unhandler.getFildOption("T_NeedForAchieve","Cooperate");
		        for(int i=0;i<cooperate.size();i++){
		        	Record rec = cooperate.get(i);
		        	fildOptionMap.put("COOPERATE"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
		        }
		        
		        RecordSet planInvest = unhandler.getFildOption("T_NeedForAchieve","PlanInvest");
		        for(int i=0;i<planInvest.size();i++){
		        	Record rec = planInvest.get(i);
		        	fildOptionMap.put("PLANINVEST"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
		        }
		        
		        RecordSet stage = unhandler.getZdxx("ITEM_16");
		        for(int i=0;i<stage.size();i++){
		        	Record rec = stage.get(i);
		        	fildOptionMap.put("ITEM_16"+rec.getString("XXDM",""),rec.getString("XXMC",""));
		        }
		        
		        
		        request.setAttribute("pagingInfo", pagingInfo);
		        request.setAttribute("kjcgNeedsSet", kjcgNeedsSet);
		        request.setAttribute("newEnerClass", newEnerClass);
		        request.setAttribute("needReason", needReason);
		        request.setAttribute("cooperate", cooperate);
		        request.setAttribute("planInvest", planInvest);
		        request.setAttribute("stage", stage);
		        
		        request.setAttribute("fildOptionMap", fildOptionMap);
		        
		        request.setAttribute("searchRcd", searchRcd);
		        
				
				forward("/back/userNeeds/KjcjNeedsManage.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
    }
    
    
    /**
     * 获取用户项目需求列表
     * 
     * @param request
     * @param response
     */
    public void handKjcgNeeds(HttpServletRequest request,
        HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        
        unhandler.handKjcgNeeds(requestKit);
    }
    
    
    /**
     * 未记未处理用户项目需求
     * 
     * @param request
     * @param response
     */
    public void unHandKjcgNeeds(HttpServletRequest request,HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        unhandler.unHandKjcgNeeds(requestKit);
    }
    
    
    
    /**
     * 删除用户专家需求
     * 
     * @param request
     * @param response
     */
    public void deleteKjcgUserNeeds(HttpServletRequest request,
        HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        
        unhandler.deleteKjcgUserNeeds(requestKit);
    }
    
    
    
    /**
     * 获取用户专家需求列表
     * 
     * @param request
     * @param response
     */
    public void userExpertNeedsList(HttpServletRequest request,
        HttpServletResponse response)
    {
    	try {
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        
		        Record searchRcd = requestKit.getRecordWithPrefix("SEA_");
		        Map provinceMap = new HashMap();
		        
		        
		        UserNeedsHandler unhandler = new UserNeedsHandler();
		        RecordSet expertNeedsSet = unhandler.userExpertNeedsList(requestKit, pagingInfo);
		        
		        RecordSet provinceSet = unhandler.getAllProvince();
		        for(int i=0;i<provinceSet.size();i++){
		        	Record rcd = provinceSet.get(i);
		        	provinceMap.put(rcd.getString("PROVINCE_ID",""), rcd.getString("PROVINCE_NAME",""));
		        }
		        
		        
		        request.setAttribute("pagingInfo", pagingInfo);
		        request.setAttribute("expertNeedsSet", expertNeedsSet);
		        request.setAttribute("provinceSet", provinceSet);
		        request.setAttribute("provinceMap", provinceMap);
		        
		        request.setAttribute("searchRcd", searchRcd);
				forward("/back/userNeeds/ExpertNeedsManage.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
    }
    
    
    
    /**
     * 处理项目需求
     * 
     * @param request
     * @param response
     */
    public void kjcgNeedsById(HttpServletRequest request,HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        
        Map fildOptionMap = new HashMap();
        
        //行业分类
        RecordSet newEnerClass = unhandler.getFildOption("common","NewEnergyClass");
        for(int i=0;i<newEnerClass.size();i++){
        	Record rec = newEnerClass.get(i);
        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
        }
        
        //需求缘由
        RecordSet needReason = unhandler.getFildOption("T_NeedForAchieve","NeedReason");
        for(int i=0;i<needReason.size();i++){
        	Record rec = needReason.get(i);
        	fildOptionMap.put("NEEDREASON"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
        }
        
        //合作方式
        RecordSet cooperate = unhandler.getFildOption("T_NeedForAchieve","Cooperate");
        for(int i=0;i<cooperate.size();i++){
        	Record rec = cooperate.get(i);
        	fildOptionMap.put("COOPERATE"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
        }
        
        //计划投入
        RecordSet planInvest = unhandler.getFildOption("T_NeedForAchieve","PlanInvest");
        for(int i=0;i<planInvest.size();i++){
        	Record rec = planInvest.get(i);
        	fildOptionMap.put("PLANINVEST"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
        }
        
        //所处阶段
        RecordSet stage = unhandler.getZdxx("ITEM_16");
        for(int i=0;i<stage.size();i++){
        	Record rec = stage.get(i);
        	fildOptionMap.put("ITEM_16"+rec.getString("XXDM",""),rec.getString("XXMC",""));
        }
        
      //机构性质
        RecordSet item_62 = unhandler.getZdxx("ITEM_62");
        for(int i=0;i<item_62.size();i++){
        	Record rec = item_62.get(i);
        	fildOptionMap.put("ITEM_62"+rec.getString("XXDM","").trim(),rec.getString("XXMC",""));
        }
        
        //地域
        Map provinceMap = new HashMap();
        RecordSet provinceSet = unhandler.getAllProvince();
        for(int i=0;i<provinceSet.size();i++){
        	Record rcd = provinceSet.get(i);
        	provinceMap.put(rcd.getString("PROVINCE_ID",""), rcd.getString("PROVINCE_NAME",""));
        }
        
        Record kjcgneed = unhandler.getKjcgNeedsById(requestKit);
        request.setAttribute("kjcgneed", kjcgneed);
        request.setAttribute("fildOptionMap", fildOptionMap);
        request.setAttribute("provinceMap", provinceMap);
        try {
        	forward("/back/userNeeds/needforachieve.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
    }
	
    
    /**
     * 处理专家需求
     * 
     * @param request
     * @param response
     */
    public void expertNeedsById(HttpServletRequest request,HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        
        Map fildOptionMap = new HashMap();
        
        //行业分类
        RecordSet newEnerClass = unhandler.getFildOption("common","NewEnergyClass");
        for(int i=0;i<newEnerClass.size();i++){
        	Record rec = newEnerClass.get(i);
        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE",""),rec.getString("OPTIONNAME",""));
        }
        
        //地域
        Map provinceMap = new HashMap();
        RecordSet provinceSet = unhandler.getAllProvince();
        for(int i=0;i<provinceSet.size();i++){
        	Record rcd = provinceSet.get(i);
        	provinceMap.put(rcd.getString("PROVINCE_ID",""), rcd.getString("PROVINCE_NAME",""));
        }
        
        Record expertneed = unhandler.getExpertNeedsById(requestKit);
        request.setAttribute("expertneed", expertneed);
        request.setAttribute("fildOptionMap", fildOptionMap);
        request.setAttribute("provinceMap", provinceMap);
        try {
        	forward("/back/userNeeds/needforexpert.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 标记未处理用户专家需求
     * 
     * @param request
     * @param response
     */
    public void unHandExpertNeeds(HttpServletRequest request,HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        unhandler.unHandExpertNeeds(requestKit);
    }
    
    
    /**
     * 删除用户专家需求
     * 
     * @param request
     * @param response
     */
    public void deleteExpertNeeds(HttpServletRequest request,HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        unhandler.deleteExpertNeeds(requestKit);
    }
	
    
    /**
     * 处理用户专家需求
     * 
     * @param request
     * @param response
     */
    public void handExpertNeeds(HttpServletRequest request,
        HttpServletResponse response)
    {
				
		RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        UserNeedsHandler unhandler = new UserNeedsHandler();
        
        unhandler.handExpertNeeds(requestKit);
    }

    
    /**
     * test
     *  
     * @param request
     * @param response
     */
    public void testFillTable(HttpServletRequest request,
        HttpServletResponse response)
    {
        try
        {
        	 UserNeedsHandler unhandler = new UserNeedsHandler();
            RecordSet rset = unhandler.testFillTable();
            
            appendString(rset.getJsonString());
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
    }
    
}
